package com.example.testproject.rv_layout_manager

import android.util.Log
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.testproject.ext.toDip

/**
 *
 * @ClassName: CardStackLayoutManager
 * @Author: android 超级兵
 * @CreateDate: 10/21/21$ 1:43 PM$
 * TODO
 */
class CardStackLayoutManager : RecyclerView.LayoutManager() {

    companion object {
        // 显示卡片总个数
        const val SHOW_CARD_COUNT = 4
        const val SCALE = 0.05f
    }

    override fun generateDefaultLayoutParams(): RecyclerView.LayoutParams =
        RecyclerView.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )


    /*
     * 作者:android 超级兵
     * 创建时间: 10/21/21 1:47 PM
     * TODO 必须重写
     */
    override fun onLayoutChildren(recycler: RecyclerView.Recycler?, state: RecyclerView.State?) {
        // super.onLayoutChildren(recycler, state)

        // 回收 复用 rv
        detachAndScrapAttachedViews(recycler!!)

        // 所有的条目
        val itemCount = itemCount


        val bottomPosition = if (itemCount < SHOW_CARD_COUNT) {
            0
        } else {
            itemCount - SHOW_CARD_COUNT
        }

        Log.i("szjItemCount", "$itemCount")


        for (i in bottomPosition until itemCount) {
            Log.i("szjI", "i:${i}")

            val view = recycler.getViewForPosition(i)

            addView(view)

            // 测量View(onMeasure) -[LinearLayoutManager]
            measureChildWithMargins(view, 0, 0)


            Log.i(
                "szjView1",
                "width${getDecoratedMeasuredWidth(view)}\theight:${getDecoratedMeasuredHeight(view)}"
            )

            Log.i(
                "szjViewRoot",
                "width${width}\theight:$height}"
            )

            // width = RootView 的宽
            // getDecoratedMeasuredWidth(view) = ItemView 的宽
            val widthSpace = width - getDecoratedMeasuredWidth(view)
            val heightSpace = height - getDecoratedMeasuredHeight(view)

            // 把所有item绘制到屏幕中间
            // 布局所有itemView(onLayout) -[LinearLayoutManager]
            layoutDecoratedWithMargins(
                view,
                widthSpace / 2,
                heightSpace / 2,
                widthSpace / 2 + getDecoratedMeasuredWidth(view),
                heightSpace / 2 + getDecoratedMeasuredHeight(view)
            )

            val level = itemCount - i - 1

            Log.i("szjLevel", "$level")

            // 对itemView缩放
            if (level > 0) {
                if (level < SHOW_CARD_COUNT - 1) {
                    view.translationY = view.context.toDip(20f) * level.toFloat()
                    view.scaleX = 1 - SCALE * level.toFloat()
                    view.scaleY = 1 - SCALE * level.toFloat()
                }
            } else {
                // 如果是最后一张，
                //
                view.translationY = view.context.toDip(20f) * (level - 1)
                view.scaleX = 1 - SCALE * (level - 1)
                view.scaleY = 1 - SCALE * (level - 1)
            }
        }
    }

}